<?php
namespace Common\Model;

/**
 * 酒店图片信息记录表
 * @modify trainyao 2015-12-31 创建
 * @index hotelpicture(`isdelete`, `hotelinfo_id`)
 */

Class HotelPictureModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'bk_hotel_picture';
    
    /**
     * 根据酒店获取图片
     * @modify trainyao 2015-12-31  创建
     * @param  integer   $hotelId 客栈id
     * @return array
     */
    public function getInfoByHotel($hotelId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );
        $fields = 'id,hotelentity_id,name';
        
        $data = $this->where($option)->field($fields)->select();
        if (empty($data)) {
            return array();
        }
        return $data;
    }
    
    /**
     * 增加客栈图片(一个或多个)
     * @modify trainyao 2015-12-31  创建
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $name 图片路径(s)
     * @return boolean 执行结果
     */
    public function addInfo($hotelId, $name) {
        $data = array();
        $result = null;
        if (!is_array($name) && strpos($name, ',') !== false) {
            $name = explode(',', $name);
        }
        
        if (is_array($name)) {
            $creator = getUserId();
            foreach ($name as $nameItem) {
                array_push($data, array(
                    'hotelentity_id' => $hotelId,
                    'name' => $nameItem,
                    'creator' => $creator,
                    'operator' => $creator,
                ));
            }
            $result = $this->addAll($data);
        } else {
            $data['hotelentity_id'] = $hotelId;
            $data['name'] = $name;
            $result = $this->add($data);
        }
        
        return $result == false ? false : true;
    }
    
    /**
     * 删除客栈相册
     * @modify trainyao 2015-12-31  创建
     * @param  mixin   $id   一个或多个id
     * @return boolean 执行结果
     */
    public function deleteInfo($id) {
        $option = array();
        if (is_array($id)) {
            $id = implode(',', $id);
        }
        if (strpos($id, ',') === false) {
            $option['id'] = $id;
        } else {
            $option['id'] = array('in', $id);
        }
        $option['isdelete'] = NOT_DELETE;

        $data = array(
            'isdelete' => IS_DELETE,
        );
        
        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 根据客栈id，url 获取图片Id
     * @modify wenhui 2016-11-29  创建
     * @param  int   $hotel 客栈id
     * @param  string   $url
     * @return array               房型数据
     */
    public function getImageInfo($hotel, $url) {
        $option = array();

        if (is_null($hotel) || is_null($url)) {
            return false;
        }
        $option['hotelentity_id'] = $hotel;
        $option['name'] = $url;
        $option['isdelete'] = NOT_DELETE;

        return $this->where($option)->find();
    }
}
